import {MockMethod} from "vite-plugin-mock";
import {userList} from "./data";

export default [
    // 获取数据
    {
        url: '/userList',
        method: 'GET',
        timeout: 985,
        response: () => {
            return {
                status: 200,
                data: userList['list'],
                message: "success",
                timestamp: Date.now(),
                total: userList['list'].length,
            }
        }
    },
    // 添加
    {
        url: '/addUser',
        method: 'POST',
        timeout: 985,
        response: ({body}) => {
            const item = userList['list'].find(item => item.id === body.id)
            if (!item) {
                userList['list'].push({...body})
                return {
                    status: 200,
                    message: "添加成功",
                }
            } else {
                return {
                    status: 401,
                    message: "添加失败",
                }
            }
        }
    },
    // 编辑
    {
        url: '/updateUser/:id',
        method: 'PUT',
        timeout: 211,
        response: ({query, body}) => {
            const index = userList['list'].findIndex(item => item.id === Number(query.id))
            if (index !== -1) {
                Object.assign(userList['list'][index], body)
                return {
                    status: 200,
                    message: "编辑成功",
                    typeofId: typeof query.id, // string
                }
            }
        }
    },
    // 删除
    {
        url: '/deleteUser/:id',
        method: "DELETE",
        timeout: 211,
        response: ({query}) => {
            const index = userList['list'].findIndex(item => item.id === Number(query.id))
            if (index !== -1) {
                userList['list'].splice(index, 1)
                return {
                    status: 200,
                    message: '删除成功',
                }
            }
        }
    },
    // 查询
    {
        url: `/query/:name`,
        method: 'GET',
        timeout: 211,
        response: ({query}) => {
            const items = userList['list'].filter(item => item.name.includes(query.name))
            if (items && items.length) {
                return {
                    status: 200,
                    data: items,
                    message: `查询到 ${items.length} 条相关数据`,
                }
            } else {
                return {
                    status: 404,
                    data: query.name,
                    message: '该用户不存在'
                }
            }
        }
    },
    // 更新状态
    {
        url: `/updateStatus/:id`,
        method: 'PUT',
        timeout: 211,
        response: ({query, body}) => {
            const index = userList['list'].findIndex(item => item.id === Number(query.id))
            if (index !== -1) {
                userList['list'][index].status = body.status
                return {
                    status: 200,
                    message: `已${body.status ? '启用' : '禁用'}`,
                }
            }
        }
    }
] as MockMethod[];